QuickOPC User's Guide and Reference
Installed Examples - Console - UASubscribeFromXml

Loads a list of OPC Unified Architecture items from an XML file and subscribes to them.

The main program:

// This example shows how to load a list of OPC Unified Architecture items from an XML file and subscribe to them.

using System;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.OperationModel;

namespace UASubscribeFromXml
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Loading items from XML file...");
            var xmlSerializer = new XmlSerializer(typeof(UAMonitoredItemArguments[]));
            var xmlReader = XmlReader.Create("UAItems.xml", new XmlReaderSettings { IgnoreWhitespace = true });
            var argArray = (UAMonitoredItemArguments[])(xmlSerializer.Deserialize(xmlReader));

            if (!(argArray is null))
            {
                Console.WriteLine();
                Console.WriteLine("Arguments loaded:");
                foreach (UAMonitoredItemArguments monitoredItemArguments in argArray)
                    Console.WriteLine($"  {monitoredItemArguments}");

                Console.WriteLine();
                Console.WriteLine("Subscribing for 30 seconds...");
                EasyUAClient.SharedInstance.SubscribeMultipleDataChanges(argArray,
                    (_, eventArgs) => Console.WriteLine(
                        $"[{eventArgs.Arguments.State}] {eventArgs.Arguments.NodeDescriptor.NodeId}: {eventArgs.AttributeData}"));
                Thread.Sleep(30 * 1000);

                Console.WriteLine();
                Console.WriteLine("Unsubscribing...");
                EasyUAClient.SharedInstance.UnsubscribeAllMonitoredItems();
            }

            Console.WriteLine();
            Console.WriteLine("Finished.");
        }
    }
}

 

The XML file with list of items:

<?xml version="1.0" encoding="utf-8"?>
<!-- This file specifies the OPC items to be subscribed to, and their associated state objects (integers, in this case). -->
<ArrayOfUAMonitoredItemArguments xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <UAMonitoredItemArguments>
    <State xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib.OperationModel">
      <TypeFullName xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib.Xml">System.Int32</TypeFullName>
      <Instance xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib.Xml">1</Instance>
    </State>
    <EndpointDescriptor xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA.OperationModel">
      <UrlString xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib">opc.tcp://opcua.demo-this.com:51210/UA/SampleServer</UrlString>
    </EndpointDescriptor>
    <NodeDescriptor xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA.OperationModel">
      <NodeId xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA">
        <NamespaceUriString xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA.AddressSpace">http://test.org/UA/Data/</NamespaceUriString>
        <NodeIdType xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA.AddressSpace">Numeric</NodeIdType>
        <NumericIdentifier xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA.AddressSpace">10849</NumericIdentifier>
      </NodeId>  
    </NodeDescriptor>
    <MonitoringParameters xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA.OperationModel">
      <SamplingInterval xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA">1000</SamplingInterval>
    </MonitoringParameters>
  </UAMonitoredItemArguments>
  <UAMonitoredItemArguments>
    <State xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib.OperationModel">
      <TypeFullName xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib.Xml">System.Int32</TypeFullName>
      <Instance xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib.Xml">2</Instance>
    </State>
    <EndpointDescriptor xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA.OperationModel">
      <UrlString xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib">opc.tcp://opcua.demo-this.com:51210/UA/SampleServer</UrlString>
    </EndpointDescriptor>
    <NodeDescriptor xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA.OperationModel">
      <NodeId xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA">
        <NamespaceUriString xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA.AddressSpace">http://test.org/UA/Data/</NamespaceUriString>
        <NodeIdType xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA.AddressSpace">Numeric</NodeIdType>
        <NumericIdentifier xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA.AddressSpace">10853</NumericIdentifier>
      </NodeId>
    </NodeDescriptor>
    <MonitoringParameters xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA.OperationModel">
      <SamplingInterval xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.UA">1000</SamplingInterval>
    </MonitoringParameters>
  </UAMonitoredItemArguments>
</ArrayOfUAMonitoredItemArguments>

 

See Also

Conceptual